We have a great Java Graph SDK sample in our tutorial document located here.  Pay special attention to the versions as not having the correct version of the prerequisites will give you difficulty!

The sample will run a console application and uses the Oauth2 Device Code flow for authentication.  Once it compiles and runs, you will get a message like this for the device code flow sign in: 

“To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code EDBKDR6TA to authenticate.” 

You can ctrl+click on the url in Visual Studio Code to open the link and you will then have a prompt to enter the highlighted device code ( your code will be different than mine ).  You only have a few minutes to complete this step before the code expires.  Once the code is accepted, you will then get a login screen to enter your credentials.  The app is polling a certain endpoint to determine if you have signed in ( see the related device code flow documentation ).  Once you are signed in, you will then get a menu in the console app screen to run some basic commands, including pressing 0 to exit.

The sample is straight forward however, occasionally, you may need to add additional header options to the request and this is not straight forward.  In my example, I am going to add sorting to the header options for the calendar events request and I also want to set the max retry count.  By default, the current versions of the Microsoft Graph clients, including the Java client, will now automatically handle the 429 error ( too many requests ) for you and retry the request up to 3 times, using the recommended wait period in the retry-after value that comes back in the response header.  After the 3rd 429 response, an exception will be thrown for that request.  For more information on 429 errors, see the Microsoft Graph throttling guidance page.  

So, in my example, I would like to increase the retry count to 5.  Please be advised that increasing the retry count will increase the wait duration for each attempt.  Setting to a number like 10 may cause a very long retry period for that last request as each attempt will increase the wait.

In the project code, under src\main\java\com\contoso is the Graph.java file.  This is where the sample has its graph requests.  There is a method for getEvents and this is the request I would like to add sorting and also change the default maxRetries option.  Since this is also a request to an Office 365 resource, I will be asking for the ImmutableId instead of the standard id ( not required but to show how this is done ). The standard request from the sample ( I did add a select parameter to only return the fields I wanted ):

public static List<Event> getEvents(String accessToken) {
ensureGraphClient(accessToken);

// Use QueryOption to specify the $orderby query parameter
final List<Option> options = new LinkedList<Option>();
// Sort results by createdDateTime, get newest first
options.add(new QueryOption("orderby", "createdDateTime DESC"));

// GET /me/events
IEventCollectionPage eventPage = graphClient
.me()
.events()
.buildRequest(options)
.select("id,subject,organizer,start,end")
.get();

return eventPage.getCurrentPage();
}

My modified version:

public static List<Event> getEvents(String accessToken) {
ensureGraphClient(accessToken);

// Use QueryOption to specify the $orderby query parameter
final List<Option> options = new LinkedList<Option>();
// Sort results by createdDateTime, get newest first
options.add(new QueryOption("orderby", "createdDateTime DESC"));

//options.add(new QueryOption("filter", "organizer eq 'ray@myemail.com'"));

options.add(new HeaderOption("maxRetries","5"));
options.add(new HeaderOption("Prefer","IdType='ImmutableId'"));

// GET /me/events
IEventCollectionPage eventPage = graphClient
.me()
.events()
.buildRequest(options)
.select("id,subject,organizer,start,end")
.get();

return eventPage.getCurrentPage();
}

This will now return sorted results, increase the retry attempts and also return an immutable id instead of the standard id.  I also have a commented out statement showing how you can filter the request, if needed.  See this related method I created to get a single CalendarEvent based on the immutable id:

public static Event getCalenderEvent(String accessToken, String id){
final List<Option> options = new LinkedList<Option>();
options.add(new HeaderOption("maxRetries","5"));
options.add(new HeaderOption("Prefer","IdType='ImmutableId'"));

Event event = graphClient
.me()
.events(id)
.buildRequest()
.select("id,subject,organizer,start,end")
.get();
return event;
}

Our GitHub for this library is located here.  Documentation for the additional request options is here.

3 Thoughts to “Configuring Options and MaxRetries for Graph Java Client”

  1. mike hill

    Please can you tell me where the code is for: ensureGraphClient(accessToken);

    1. Ray Held [MSFT]

      I have published the source code for this blog post here: https://github.com/RayGHeld/Java_GraphClient/tree/master

      1. mike hill

        Many thanks

Leave a Comment